package com.briup.api.client.fallback;

import com.briup.api.client.ItemClient;
import com.briup.api.po.Goods;
import com.briup.api.po.dto.OrderItemInsertDTO;
import com.briup.common.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Slf4j
public class ItemClientFallbackFactory implements FallbackFactory<ItemClient> {

    @Override
    public ItemClient create(Throwable cause) {
        return new ItemClient() {
            @Override
            public List<Goods> queryItemByIds(Collection<Integer> ids) {
                //输出错误日志
                log.error("查询商品信息失败！", cause);
                //返回空集合
                return new ArrayList<>();
            }

            @Override
            public void updateStock(List<OrderItemInsertDTO> items) {
                log.error("扣减商品库存失败!",cause);
                //目前不了解扣减库存业务，所以直接抛出异常
                throw new RuntimeException(cause);
            }

            @Override
            public Result queryItemById(Integer id) {
                log.error("查询商品失败!",cause);
                //直接抛出异常
                throw new RuntimeException(cause);
            }
        };
    }
}
